<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
require_once ('businessfunctions.php');
validate_user();

html_header(); ?>

<table>
    <tr bgcolor='<? echo $color3 ?>' align="center">
    <td abbr='20%' ><b><? echo $langbillmanagement ?>
    <td width='10%'><font color='$color4'><a href = 'billprinting.php'><? echo $langmanagement?></a>
    <td width='10%'><font color='$color4'><a href = 'bill_list'><? echo $langsearch?></a>
</table><br>

<?
$sql =  "SELECT a.companyid, a.billno, billdate, SUM( a.chargerate * a.quantity ) AS totalamount,
         SUM( a.chargerate * a.quantity / 100 * a.taxrate) AS totalvat, a.clientid, b.duedate, b.paydate
         FROM billtransaction AS a
         LEFT JOIN billdata AS b
         ON a.billno = b.billno
         AND a.companyid = b.companyid
         AND LEFT(a.billdate,4) = b.year
         WHERE a.billno IS NOT NULL
         GROUP BY a.companyid, a.billdate, a.billno
         ORDER BY a.companyid, a.billdate DESC , a.billno DESC";
$sql_result = sql_execute($sql);

// Do some preprocessing for calculating totals
// ...
// ...

column_headings(array($langcompanyid, $langbillno, $langbilldate, $langduedate, $langpaydate,
                $langtaxable, $langamount." ".$langVAT , $langamount, $langclientid));
while ($sql_data = mysql_fetch_array($sql_result)) {
       $rowcolor = row_color_change();
       echo "\n<tr bgcolor='$rowcolor'>";
       echo "<td nowrap>";
       echo $sql_data['companyid'];
       echo "<td nowrap>";
       echo $sql_data['billno'];
       echo "<td nowrap>";
       echo convert_mysql_date4output($sql_data['billdate']);
       echo "<td nowrap";
       if ($sql_data['duedate']<=date("Y-m-d") && $sql_data['paydate']=="0000-00-00")
       		echo " bgcolor='red'";
       echo ">";
       echo convert_mysql_date4output($sql_data['duedate']);
       echo "<td nowrap>";
       echo convert_mysql_date4output($sql_data['paydate']);
       echo "<td nowrap align='right'>";
       echo number_format($sql_data['totalamount'],2,".","");
       echo "<td nowrap align='right'>";
       echo number_format($sql_data['totalvat'],2,".","");
       echo "<td nowrap align='right'>";
       echo number_format($sql_data['totalamount']+$sql_data['totalvat'],2,".","");
       echo "<td nowrap>";
       echo $sql_data['clientid'];
       echo "<td nowrap>";
       echo "<a href=javascript:void(window.open('mybillprinting.php?CustomNumber=".$sql_data['billno']."&SumExp=e&BillYear=".substr($sql_data['billdate'],0,4)."&BillCompanyID=".$sql_data['companyid']."'))>";
       echo "<img src='img/printer.gif' border=0></a>";
       echo "<a href=\"#\" onclick=\"window.open('bill_data.php?billno=".$sql_data['billno']."&year=".substr($sql_data['billdate'],0,4)."&companyid=".$sql_data['companyid']."','billdata','resizable=1, toobars=no, scrollbars=1, width=700, height=400')\">";
       echo "<img src='img/edit.gif' border=0></a>";
}
       echo "</td></tr></table>";

html_footer();
?>